package com.user.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.user.entity.Department;
import com.user.entity.dto.DepartmentDTO;
import com.user.service.IDepartmentService;

@Controller
@RequestMapping("/dept")
public class DepartmentController {
	@Autowired
	private IDepartmentService departmentService;
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Department> findAll(){
		try {
			return departmentService.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(Department department) {
		try {
			if(departmentService.isNumExist(department))
				return new AjaxResponse(false, "编号已存在!");
			departmentService.saveOrUpdate(department);
			return new AjaxResponse(true, "操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id) {
		try {
			if(departmentService.hasTeacher(id))
				return new AjaxResponse(false, "院系存在教师，请先移除关系!");
			if(departmentService.hasStudent(id))
				return new AjaxResponse(false, "院系存在学生，请先移除关系!");
			departmentService.delete(id);
			return new AjaxResponse(true, "已删除！");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "删除失败...");
		}
	}
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<Department> findPage(String str,JpaPageable pageable){
		try {
			return departmentService.findTablePage(DepartmentDTO.getSpecification(str), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findList")
	public @ResponseBody List<Department> findList(){
		return departmentService.findAll();
	}
}
